package cn.lsnu.modules.login.controller;


import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.modules.login.entity.LoginEntity;
import cn.lsnu.modules.login.service.LoginService;
import cn.lsnu.utils.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Description 登录页面
 * @author  lin
 * @Datetime  2020/2/1 17:39
 */
@Controller
@RequestMapping({"/login/weblogin","/app"})
public class LoginController extends BaseController {

    @Resource(name="loginService")
    private LoginService loginService;

    //日志消息打印
    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @RequestMapping(value="/save",method = RequestMethod.POST)
    public String loginWeb(LoginEntity loginEntity) {

        try {
            loginService.save(loginEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return  "index";
    }
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    public String login(Model model, LoginEntity loginEntity,
                        HttpServletRequest request,
                        HttpSession session) throws Exception {
        String result = loginService.loginDealResult(loginEntity,session);
        if(result.equals("SUCCESS")){
            model.addAttribute("msg","登录成功");
            return  "webpage/common/main/index";
        }
        model.addAttribute("msg",result);
        System.out.println(result);
        return  "redirect:/webpage/common/login/login.jsp";
    }

    @RequestMapping(value = "/login/users",method = RequestMethod.POST)
    public @ResponseBody
            LoginEntity appLogin(LoginEntity entity, HttpSession session) throws Exception {
        String result = loginService.loginDealResult(entity,session);
        entity.setUserPwd(null);
        if(result.equals("SUCCESS")){
            entity.setCode(Global.STATUE_SUCCESS);
        }else{
            entity.setMsg(result);
            entity.setCode(Global.STATUE_FAIL);
        }


        return entity;
    }

    @RequestMapping("/loginpage")
    public String loginPage(){


        return "webpage/common/main/index";
    }

    /*
    * 注销登录
    * */
    @RequestMapping("/logout")
    public String logOut(HttpSession session){
        session.invalidate();
        return "redirect:/webpage/common/login/login.jsp";
    }

    @RequestMapping("/users/get")
    public @ResponseBody
        LoginEntity get(@RequestParam("id") String key) throws  Exception{
        return loginService.get(key);
    }

    @RequestMapping("/users/upd")
    public @ResponseBody
    LoginEntity upd(LoginEntity entity) throws  Exception{
        loginService.update(entity);
        entity.setCode(Global.STATUE_SUCCESS);
        entity.setMsg("修改成功");
        return entity;
    }

}
